//***********************************************************************
//	File:		Options.cpp
//	Author:		
//	Course:		SGP 1405
//	Purpose:	
//***********************************************************************
//***********************************************************************
//	Purposfully left blank for all comments regarding this file 
//	
//	
//	
//***********************************************************************

#include <sstream>

#include "Options.h"
#include "../source/Game.h"
#include "../GameStates/MainMenu.h"
#include "../source/BitmapFont.h"


#include "../SGD Wrappers/SGD_AudioManager.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "../SGD Wrappers/SGD_InputManager.h"

#include "../TinyXML/tinyxml.h"



Options* Options::GetInstance()
{
	static Options s_pInstance; 
	return &s_pInstance; 
}


void Options::Enter(void)
{
	m_hSampleMaster = Game::GetInstance()->GetBackgroundMus(); 
	m_hSampleSfx = Game::GetInstance()->GetSampleSfx(); 

	m_nMasterVolume = SGD::AudioManager::GetInstance()->GetMasterVolume(SGD::AudioGroup::Music);
	m_nSfxVolume = SGD::AudioManager::GetInstance()->GetMasterVolume(SGD::AudioGroup::SoundEffects);
}

void Options::Exit(void)
{
	SGD::AudioManager* pAudio = SGD::AudioManager::GetInstance(); 

	if (pAudio->IsAudioPlaying(m_hSampleMaster) == true)
	{
		pAudio->StopAudio(m_hSampleMaster); 
	}
	if (pAudio->IsAudioPlaying(m_hSampleSfx) == true)
	{
		pAudio->StopAudio(m_hSampleSfx); 
	}

	TiXmlDocument doc; 

	TiXmlDeclaration* pDecl = new TiXmlDeclaration("1.0", "utf-8", ""); 
	
	doc.LinkEndChild(pDecl); 

	TiXmlElement* pRoot = new TiXmlElement("SavedOptions"); 

	// Attach to the Root
	doc.LinkEndChild(pRoot); 

	TiXmlElement*  pMasterVolume = new TiXmlElement("MasterVolume"); 
	pMasterVolume->SetAttribute("MasterVolume", m_nMasterVolume); 
	pRoot->LinkEndChild(pMasterVolume); 

	TiXmlElement* pSfxVolume = new TiXmlElement("SFXVolume");
	pSfxVolume->SetAttribute("SfxVolume", m_nSfxVolume); 
	pRoot->LinkEndChild(pSfxVolume); 

	TiXmlElement* pFullscreen = new TiXmlElement("Fullscreen"); 
	pFullscreen->SetAttribute("Fullscreen", m_bIsFullscreen); 
	pRoot->LinkEndChild(pFullscreen); 

	doc.SaveFile("resource/data/SavedOptions.xml"); 
}

bool Options::Input(void)
{
	SGD::InputManager* pInput = SGD::InputManager::GetInstance(); 
	if (SGD::InputManager::GetInstance()->IsKeyPressed(SGD::Key::Escape) == true)
	{
		Game::GetInstance()->ChangeState(MainMenu::GetInstance());
	}
	if (pInput->IsKeyPressed(SGD::Key::Up) == true ||
		pInput->IsDPadPressed(0, SGD::DPad::Up) == true ||
		pInput->IsDPadPressed(1, SGD::DPad::Up) == true)
	{
		m_unCursor--; 
		if (m_unCursor < 0)
			m_unCursor = m_unCursorMax; 
	}
	if (pInput->IsKeyPressed(SGD::Key::Down) == true || 
		pInput->IsDPadPressed(0,SGD::DPad::Down) == true ||
		pInput->IsDPadPressed(1, SGD::DPad::Down) == true)
	{
		m_unCursor++; 
		if (m_unCursor > m_unCursorMax)
			m_unCursor = 0; 
	}
	if (pInput->IsKeyPressed(SGD::Key::Right) == true || 
		pInput->IsDPadPressed(0,SGD::DPad::Right) == true ||
		pInput->IsDPadPressed(1,SGD::DPad::Right))
	{
		switch (m_unCursor)
		{
			case MASTER_VOLUME:
			{
				m_nMasterVolume += 5;
				if (m_nMasterVolume > 100)
					m_nMasterVolume = 100; 
			}
				break;
			case SFX_VOLUME:
			{
				m_nSfxVolume += 5;
				if (m_nSfxVolume > 100)
					m_nSfxVolume = 100; 
			}
				break; 
		};
	}
	
	if (pInput->IsKeyPressed(SGD::Key::Left) == true ||
		pInput->IsDPadPressed(0,SGD::DPad::Left) == true
		|| pInput->IsDPadPressed(1,SGD::DPad::Left) == true)
	{

		switch (m_unCursor)
		{
			case MASTER_VOLUME:
			{
				m_nMasterVolume -= 5; 
				if (m_nMasterVolume < 0)
					m_nMasterVolume = 0; 
			};
				break; 
			case SFX_VOLUME:
			{
				m_nSfxVolume -= 5; 
				if (m_nSfxVolume < 0)
					m_nSfxVolume = 0; 
			}; 
		}
	}
	if (pInput->IsKeyPressed(SGD::Key::Enter) == true || 
		pInput->IsButtonPressed(0,0) == true || 
		pInput->IsButtonPressed(1, 0) == true)
	{
		switch (m_unCursor)
		{
			case FULLSCREEN:
			{
				m_bIsFullscreen = !m_bIsFullscreen;
			}
				break; 
			case EXIT:
			{
				Game::GetInstance()->ChangeState(MainMenu::GetInstance());
			}
				break; 
		}
	}

	return true; 
}

void Options::Update(float dt)
{
	SGD::AudioManager* pAudio = SGD::AudioManager::GetInstance();

	SGD::GraphicsManager* pGraphics = SGD::GraphicsManager::GetInstance(); 
	if (m_bIsFullscreen)
		pGraphics->Resize({ 1024, 768 }, false);
	else
		pGraphics->Resize({ 1024, 768 }, true);

	if (m_unCursor == MASTER_VOLUME && !pAudio->IsAudioPlaying(m_hSampleMaster))
	{
		pAudio->PlayAudio(m_hSampleMaster);
	}

	if (m_unCursor == SFX_VOLUME && pAudio->IsAudioPlaying(m_hSampleMaster))
	{
		pAudio->StopAudio(m_hSampleMaster);
	}
	
	if (m_unCursor == SFX_VOLUME && !pAudio->IsAudioPlaying(m_hSampleSfx))
	{
		pAudio->PlayAudio(m_hSampleSfx);
	}

	if (m_unCursor == MASTER_VOLUME && pAudio->IsAudioPlaying(m_hSampleSfx))
	{
		pAudio->StopAudio(m_hSampleSfx); 
	}

	if (m_unCursor != MASTER_VOLUME && m_unCursor != SFX_VOLUME)
	{
		if (pAudio->IsAudioPlaying(m_hSampleMaster))
		{
			pAudio->StopAudio(m_hSampleMaster);
		}
		if (pAudio->IsAudioPlaying(m_hSampleSfx))
		{
			pAudio->StopAudio(m_hSampleSfx);
		}
	}

	pAudio->SetMasterVolume(SGD::AudioGroup::Music, m_nMasterVolume);
	pAudio->SetMasterVolume(SGD::AudioGroup::SoundEffects, m_nSfxVolume);


}

void Options::Render(void)
{
	int width = (int)(Game::GetInstance()->GetScreenWidth());
	int height = (int)(Game::GetInstance()->GetScreenHeight());
	BitmapFont* pFont = Game::GetInstance()->GetFont(); 

	std::ostringstream masterVolumeControl; 
	std::ostringstream sfxVolumeControl; 

	masterVolumeControl << "Music Volume: " << m_nMasterVolume; 
	sfxVolumeControl << "Sfx Volume: " << m_nSfxVolume; 
	

	//unhardcode these strings
	pFont->Draw("Options", (width / 2) - 4, 
		height / 2 - 60, 1, SGD::Color(255, 255, 200, 0)); 

	if (m_unCursor == MASTER_VOLUME)
	{
		pFont->Draw(masterVolumeControl.str().c_str(), (width / 2) - 4,
			(height / 2 - 60) + 50, 1, SGD::Color(255, 255, 200, 0));
	}
	else
	{
		pFont->Draw(masterVolumeControl.str().c_str(), (width / 2) - 4,
			(height / 2 - 60) + 50, 1, SGD::Color(255, 255, 0, 0));
	}

	if (m_unCursor == SFX_VOLUME)
		pFont->Draw(sfxVolumeControl.str().c_str(), (width / 2) - 4,
		(height / 2 - 60) + 100, 1, SGD::Color(255, 255, 200, 0));
	else 
		pFont->Draw(sfxVolumeControl.str().c_str(), (width / 2) - 4,
		(height / 2 - 60) + 100, 1, SGD::Color(255, 255, 0, 0));

	if (m_unCursor == FULLSCREEN)
		pFont->Draw("Fullscreen", (width / 2) - 4,
		(height / 2 - 60) + 150, 1, SGD::Color(255, 255, 200, 0));
	else
		pFont->Draw("Fullscreen", (width / 2) - 4,
		(height / 2 - 60) + 150, 1, SGD::Color(255, 255, 0, 0));

	if (m_unCursor == EXIT)
		pFont->Draw("Exit", (width / 2) - 4,
		(height / 2 - 60) + 200, 1, SGD::Color(255, 255, 200, 0));
	else
		pFont->Draw("Exit", (width / 2) - 4,
		(height / 2 - 60) + 200, 1, SGD::Color(255, 255, 0, 0));


}